const host = 'http://localhost:8000'
// 服务器使用：
// const host = 'http://{域名或ip}:8000'

function back() {
    location.href = './book.html'
}

function save() {
    const id = new URLSearchParams(location.search).get('id')
    const bookName = $('[name=bookName]').val()
    const author = $('[name=author]').val()
    const descriptions = $('[name=descriptions]').val()
    if (id) {
        axios.put(`${host}/books/${id}`, { bookName, author, descriptions }).then(res => {
            if (res.data.code === 1000) {
                alert('修改成功')
                location.href = './book.html'
            }
        })
    } else {
        axios.post(`${host}/books`, { bookName, author, descriptions }).then(res => {
            if (res.data.code === 1000) {
                alert('添加成功')
                location.href = './book.html'
            }
        })
    }
}
//渲染网页
$(function () {
    const id = new URLSearchParams(location.search).get('id')
    if (id) {
        axios.get(`${host}/books/${id}`).then(res => {
            const book = res.data.data
            //拿到数据了，准备渲染
            $('[name=bookName]').val(book.bookName)
            $('[name=author]').val(book.author)
            $('[name=descriptions]').val(book.descriptions)
        })
    }
})